package com.zhao.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.zhao.pojo.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;



public class XlxsTool {

    private static final Logger log = LoggerFactory.getLogger(XlxsTool.class);

    public static <T> List<T> loadExcel(String excelPath, Class<T> clazz) {
        try (FileInputStream fis = new FileInputStream(excelPath)) {
            return loadExcel(fis, clazz);
        } catch (Exception xe) {
            throw new RuntimeException(xe);
        }
    }

    public static <T> List<T> loadExcel(InputStream inputStream, Class<T> clazz) {
        ExcelDataListener<T> excelDataListener = new ExcelDataListener<>();
        EasyExcel.read(inputStream, clazz, excelDataListener).sheet().doRead();
        return excelDataListener.getResult();
    }

    public static <T> Map<String, List<T>> loadAllExcel(String excelPath, Class<T> clazz) {
        try (FileInputStream fis = new FileInputStream(excelPath)) {
            return loadAllExcel(fis, clazz);
        } catch (Exception xe) {
            throw new RuntimeException(xe);
        }
    }

    public static <T> Map<String, List<T>> loadAllExcel(InputStream inputStream, Class<T> clazz) {
        ExcelDataAllSheetListener<T> excelDataListener = new ExcelDataAllSheetListener<>();
        EasyExcel.read(inputStream, clazz, excelDataListener).doReadAll();
        return excelDataListener.getResult();
    }

    public static <T> void export(String filePath, Class<T> type, List<T> mainExcelData) {
        EasyExcel.write(filePath, type).sheet().doWrite(mainExcelData);
    }

    public static <T> void export(String filePath, Class<T> type, Map<String, List<T>> mainExcelData) {
        ExcelWriter excelWriter = EasyExcel.write(filePath, type).build();
        AtomicInteger i = new AtomicInteger();
        mainExcelData.forEach((key, value) -> {
            WriteSheet writeSheet = EasyExcel.writerSheet(i.getAndIncrement(), key).build();
            excelWriter.write(value, writeSheet);
        });
        excelWriter.finish();
    }

    public static class ExcelDataListener<T> extends AnalysisEventListener<T> {
        private final List<T> result = new ArrayList<>();

        @Override
        public void invoke(T data, AnalysisContext context) {
            result.add(data);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {

        }

        public List<T> getResult() {
            return result;
        }
    }

    public static class ExcelDataAllSheetListener<T> extends AnalysisEventListener<T> {
        private final Map<String, List<T>> result = new LinkedHashMap<>();

        @Override
        public void invoke(T data, AnalysisContext context) {
            ReadSheetHolder readSheetHolder = context.readSheetHolder();
            String sheetName = readSheetHolder.getSheetName();
            List<T> ts = result.computeIfAbsent(sheetName, k -> new ArrayList<>());
            ts.add(data);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {

        }

        public Map<String, List<T>> getResult() {
            return result;
        }
    }

    public static void main(String[] args) {
        List<Student> dataList = XlxsTool.loadExcel("C:\\Users\\赵东旭\\Desktop/11111.xlsx", Student.class);
        System.out.println(dataList);
    }
}
